Option Strict Off
Imports System
Imports system.IO
Imports system.ComponentModel
Imports System.xml.Serialization
Imports System.Windows.Forms

Public Class SerializeMatrixtoXML

    Private Matrix As MatrixDataStructures.SingleVolume
    'Dim IntMatrix As MatrixDataStructures.Matrix3DInt16
    'Dim SngMatrix As MatrixDataStructures.Matrix3DSingle

    'Public Enum MatrixType
    '    Int16Matrix
    '    SingleMatrix
    'End Enum
    'Dim Choice As MatrixType = MatrixType.Int16Matrix

    Dim XmlSerializer As XmlSerializer
    Dim StringWriter As New StringWriter()

    Public Sub SaveAs(ByVal Volume As MatrixDataStructures.SingleVolume)
        Dim SFD As New Windows.Forms.SaveFileDialog
        SFD.AddExtension = True
        SFD.Title = "Save XML file as ..."
        SFD.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*"
        If SFD.ShowDialog = DialogResult.OK Then
            'FormMain.StartBusyBoxThread("Saving data to XML")
            Me.Save(Volume, SFD.FileName)
            'FormMain.StopBusyBoxThread()
        End If
    End Sub
    Public Sub Save(ByVal Volume As MatrixDataStructures.SingleVolume, ByVal fileName As String)
        Me.Matrix = Volume
        Dim XMLCLass = New XmlDataClass(Matrix)
        Dim writer As New XmlSerializer(XMLCLass.GetType)
        Dim file As New StreamWriter(fileName)
        writer.Serialize(file, XMLCLass)
        file.Close()
    End Sub

    Private Sub LoaComputedMatrix()
        Dim OFD As New Windows.Forms.OpenFileDialog
        OFD.AddExtension = True
        OFD.Title = "Open XML file ..."
        OFD.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*"
        If OFD.ShowDialog = DialogResult.OK Then
            'FormMain.StartBusyBoxThread("Loading Data from XML")
            Me.LoadFromFile(OFD.FileName)
        Else
        End If
    End Sub
    Private Sub LoaComputedMatrix(ByVal filename As String)
       Dim XMLClass As XmlDataClass
            XMLClass = New XmlDataClass
        Dim reader As New XmlSerializer(XMLClass.GetType)
        Dim file As New StreamReader(filename)
        XMLClass = CType(reader.Deserialize(file), XmlDataClass)

        Me.Matrix = XMLClass.returnvolume
    End Sub
    Public Function LoadFromFile() As MatrixDataStructures.SingleVolume
        LoaComputedMatrix()
        Return Me.Matrix
    End Function
    Public Function LoadFromFile(ByVal filename As String) As MatrixDataStructures.SingleVolume
        LoaComputedMatrix(filename)
        Return Me.Matrix
    End Function


End Class

<XmlRoot(ElementName:="MetaViewDataSet")> _
<Serializable()> Public Class XmlDataClass
    ' Attributes are created as strings.
    ' <XmlElement("Something"), XmlAttribute("MatrixSizeM")> Public SizeM() As Integer
    ' <XmlAttribute("Matrix")> Public Matrix() As Integer
    ' <XmlAttribute("MatrixSizeMD")> Public SizeMD() As Integer
    ' <XmlAttribute("MatrixD")> Public MatrixD() As Single
    <XmlAttribute("FOV")> Public FOV() As Integer
    <XmlAttribute("Center")> Public Center() As Integer
    <XmlAttribute("HeaderFile")> Public HeaderFileName As String

    Public IntegerMatrix As XMLMatrix
    Public ComputedMatrix As XMLMatrixD

    Public Sub New()
    End Sub

    Public Sub New(ByVal volume As MatrixDataStructures.SingleVolume)
        If volume.Matrix Is Nothing Then
            volume.Matrix = New MatrixDataStructures.Matrix3DInt16(0, 0, 0)
        End If
        If volume.MatrixD Is Nothing Then
            volume.MatrixD = New MatrixDataStructures.Matrix3DSingle(0, 0, 0)
        End If
        Me.IntegerMatrix = New XMLMatrix(volume.Matrix)
        Me.ComputedMatrix = New XMLMatrixD(volume.MatrixD)

        ReDim Me.FOV(2)

        Me.FOV(0) = volume.FOV.x
        Me.FOV(1) = volume.FOV.y
        Me.FOV(2) = volume.FOV.z

        ReDim Me.Center(2)
        Me.Center(0) = volume.Center.x
        Me.Center(1) = volume.Center.y
        Me.Center(2) = volume.Center.z
        Me.HeaderFileName = volume.HeaderFileName
    End Sub


    Public Function returnvolume() As MatrixDataStructures.SingleVolume

        Dim data As New MatrixDataStructures.SingleVolume

        data.Matrix = IntegerMatrix.returnmatrix
        
        data.MatrixD = ComputedMatrix.returnmatrix

        Dim Temp As New MatrixDataStructures.CoordinateD(Me.FOV(0), Me.FOV(1), Me.FOV(2))
        data.FOV = Temp

        Dim Temp2 As New MatrixDataStructures.CoordinateD(Me.Center(0), Me.Center(1), Me.Center(2))
        data.Center = Temp2

        data.HeaderFileName = Me.HeaderFileName

        Return data
    End Function

    ' THE FOLLOWING IS ALWAYS REQUIRED
    ' Text is added in a non-displayed tag called Value.
    <XmlText()> Public Value As System.String
End Class

<XmlRoot(ElementName:="Matrix")> _
<Serializable()> Public Class XMLMatrix
    ' Attributes are created as strings.
    <XmlAttribute("Size")> Public SizeM() As Integer
    <XmlAttribute("Data")> Public Matrix() As Integer

    Public Sub New()

    End Sub
    Public Sub New(ByVal DataMatrix As MatrixDataStructures.Matrix3DInt16)

        Dim temp(CDbl(DataMatrix.x) * CDbl(DataMatrix.y) * CDbl(DataMatrix.z)) As Integer
        Dim counter As Double = 0
        For i As Integer = 0 To DataMatrix.x - 1
            For j As Integer = 0 To DataMatrix.y - 1
                For k As Integer = 0 To DataMatrix.z - 1
                    temp(counter) = DataMatrix.data(i, j, k)
                    counter += 1
                Next
            Next
        Next
        Me.Matrix = temp

        ReDim Me.SizeM(2)
        Me.SizeM(0) = DataMatrix.x
        Me.SizeM(1) = DataMatrix.y
        Me.SizeM(2) = DataMatrix.z

    End Sub


    Public Function returnmatrix() As MatrixDataStructures.Matrix3DInt16
        Dim data = New MatrixDataStructures.Matrix3DInt16(Me.SizeM(0), Me.SizeM(1), Me.SizeM(2))
        Dim counter As Double = 0
        For i As Integer = 0 To Me.SizeM(0) - 1
            For j As Integer = 0 To Me.SizeM(1) - 1
                For k As Integer = 0 To Me.SizeM(2) - 1
                    data(i, j, k) = Me.Matrix(counter)
                    counter += 1
                Next
            Next
        Next
        Return data
    End Function

    ' THE FOLLOWING IS ALWAYS REQUIRED
    ' Text is added in a non-displayed tag called Value.
    <XmlText()> Public Value As System.String
End Class
<XmlRoot(ElementName:="MatrixD")> _
<Serializable()> Public Class XMLMatrixD
    ' Attributes are created as strings.
    <XmlAttribute("Size")> Public SizeMD() As Integer
    <XmlAttribute("Data")> Public MatrixD() As Single

    Public Sub New()
    End Sub
    Public Sub New(ByVal DataMatrix As MatrixDataStructures.Matrix3DSingle)

        Dim temp(CDbl(DataMatrix.x) * CDbl(DataMatrix.y) * CDbl(DataMatrix.z)) As Single
        Dim counter As Double = 0
        For i As Integer = 0 To DataMatrix.x - 1
            For j As Integer = 0 To DataMatrix.y - 1
                For k As Integer = 0 To DataMatrix.z - 1
                    temp(counter) = DataMatrix.data(i, j, k)
                    counter += 1
                Next
            Next
        Next
        Me.MatrixD = temp

        ReDim Me.SizeMD(2)
        Me.SizeMD(0) = DataMatrix.x
        Me.SizeMD(1) = DataMatrix.y
        Me.SizeMD(2) = DataMatrix.z

    End Sub


    Public Function returnmatrix() As MatrixDataStructures.Matrix3DSingle

        Dim data = New MatrixDataStructures.Matrix3DSingle(Me.SizeMD(0), Me.SizeMD(1), Me.SizeMD(2))
        Dim counter As Double = 0
        For i As Integer = 0 To Me.SizeMD(0) - 1
            For j As Integer = 0 To Me.SizeMD(1) - 1
                For k As Integer = 0 To Me.SizeMD(2) - 1
                    data(i, j, k) = Me.MatrixD(counter)
                    counter += 1
                Next
            Next
        Next
        Return data
    End Function

    ' THE FOLLOWING IS ALWAYS REQUIRED
    ' Text is added in a non-displayed tag called Value.
    <XmlText()> Public Value As System.String
End Class